﻿
using System.Collections.Generic;

namespace Anno.DynamicApi
{
    /// <summary>
    /// 出参信息
    /// </summary>
    public class ApiOutputInfo
    {
        /// <summary>
        /// 返回名称
        /// </summary>
        public string OutputName { get; set; }
        /// <summary>
        /// 是否返回数组
        /// </summary>
        public bool IsArray { get; set; } = true;

        /// <summary>
        /// 数据源下标,默认0  如：ds.DataTales[DataSoureIndex] 
        /// </summary>
        public int DataSoureIndex { get; set; } = 0;
        ///// <summary>
        ///// 数据源名称  如：DataTaleName ,DataSoureIndex 和 DataSoureName 二者取其一，DataSoureIndex不为大于0时优先
        ///// </summary>
        //public string DataSoureName { get; set; } = "";

        /// <summary>
        /// 接口名称
        /// </summary>
        public string ApiName { get; set; }
        /// <summary>
        /// 返回实体Class名称,动态生成 类时用
        /// </summary>
        public string OutputClassName { get { return $"_{OutputName }_Output"; } }

        public List<ApiFieldInfo> ApiInputInfoList { get; set; }

        /// <summary>
        /// 接口是否有出实体
        /// </summary>
        public bool HasOutput { get { return this.ApiInputInfoList?.Count > 0; } }

        public ApiOutputInfo() { }
        public ApiOutputInfo(System.Xml.XmlNode xmlNode)
        {
            if (xmlNode == null | xmlNode.NodeType == System.Xml.XmlNodeType.Comment)
            {
                return;
            }
            Init(xmlNode);
        }
        public ApiOutputInfo(Newtonsoft.Json.Linq.JToken outputNode)
        {
            if (outputNode == null)
            {
                return;
            }
            Init(outputNode);
        }
        public void Init(System.Xml.XmlNode outputNode)
        {
            try
            {
                if (outputNode == null || outputNode.NodeType == System.Xml.XmlNodeType.Comment)
                {
                    return;
                }

                OutputName = outputNode.GetXmlNodeAttributeValue("OutputName");
                DataSoureIndex = outputNode.GetXmlNodeAttributeValue("DataSoureIndex", "0").NullToInt();
                IsArray = outputNode.GetXmlNodeAttributeValue("IsArray", "true").ChangeType<bool>();
                if (DataSoureIndex < 0) DataSoureIndex = 0;
            }
            catch { }
        }
        public void Init(Newtonsoft.Json.Linq.JToken outputNode)
        {
            try
            {
                if (outputNode == null)
                {
                    return;
                }
                OutputName = outputNode.GetXmlNodeAttributeValue("OutputName");
                DataSoureIndex = outputNode.GetXmlNodeAttributeValue("DataSoureIndex", "0").NullToInt();
                IsArray = outputNode.GetXmlNodeAttributeValue("IsArray", "true").ChangeType<bool>();
                if (DataSoureIndex < 0) DataSoureIndex = 0;
            }
            catch { }
        }
        /// <summary>
        /// 返回JSON对象
        /// </summary>
        public virtual object JObject
        {
            get
            {
                var dyret = DicAttrs;

                var apiInputInfoList = new System.Collections.Generic.List<object>();
                ApiInputInfoList?.ForEach(ext => apiInputInfoList.Add(ext.JObject));
                dyret["ApiInputInfoList"] = apiInputInfoList;

                dyret["HasOutput"] = HasOutput;
                return dyret;

                //var apiInputInfoList = new System.Collections.Generic.List<object>();
                //ApiInputInfoList?.ForEach(ext => apiInputInfoList.Add(ext.JObject));

                //return new
                //{
                //    OutputName,
                //    DataSoureIndex,
                //    IsArray,
                //    HasOutput,
                //    ApiInputInfoList = apiInputInfoList
                //};
            }
        }

        public virtual System.Collections.Generic.IDictionary<string, object> DicAttrs
        {
            get
            {
                var ret = new System.Dynamic.ExpandoObject();
                System.Collections.Generic.IDictionary<string, object> dyret = ret;

                //var apiInputInfoList = new System.Collections.Generic.List<System.Collections.Generic.IDictionary<string, object>>();
                //ApiInputInfoList?.ForEach(ext => apiInputInfoList.Add(ext.DicAttrs));

                dyret["OutputName"] = OutputName;
                dyret["DataSoureIndex"] = DataSoureIndex;
                dyret["IsArray"] = IsArray;
                //dyret["ApiInputInfoList"] = apiInputInfoList;
                //dyret["HasOutput"] = HasOutput;

                return ret;
            }
        }
    }
}